#include "iot_business_process.h"

IOTBusinessProcess::IOTBusinessProcess(string _host,int _port)
{
    iot_mqtt_entity_ = new MosquittoAPI(_host.c_str(),_port);
    iot_business_process_init();
}

IOTBusinessProcess::~IOTBusinessProcess()
{
    delete iot_mqtt_entity_;
}

void IOTBusinessProcess::iot_business_process_init()
{
     iot_mqtt_entity_->AddCallback(&IOTBusinessProcess::iot_recv_message_callback, this);
     iot_mqtt_entity_->SubscribeTopic("example/temperature", 1);
}

void IOTBusinessProcess::iot_recv_message_callback(const  mosquitto_message *msg)
{
    string topic(msg->topic);
    std::map<std::string, std::function<void(const  mosquitto_message *)>> actions = {
        {"example/temperature", std::bind(&IOTBusinessProcess::iot_temp_process, this, std::placeholders::_1)},
        {"example/temperature2",  std::bind(&IOTBusinessProcess::iot_temp_process, this, std::placeholders::_1)},
    };

    auto it = actions.find(topic);
    if (it != actions.end()) {
        it->second(msg);  
    } else {
        PrintI("topic %s not exit in list",msg->topic);
    }
}

void IOTBusinessProcess::iot_send_message(char *topic, void *message)
{
    iot_mqtt_entity_->PublishMessage(message, topic, 1);
}

bool IOTBusinessProcess::iot_temp_process(const mosquitto_message *msg)
{
    PrintI("topic %s ,payload %s,payloadlen %d",msg->topic,msg->payload,msg->payloadlen);
    return true;
}

void IOTBusinessProcess::iot_business_process_start()
{
}

